<?php
namespace app\admin\controller;
use think\view;
use think\Db;
use think\Session;
use Qiniu\Auth as Auth;
use think\Cache;
class Order extends CommonController
{
    //用户管理
    public function index()
    {
        if($_GET['search']){
            $search =  $_GET['search'];

            if($search==null){
                $list = '';
            }else{

                $list = DB::name('order')
                    ->where('title','like','%'.$search.'%')
                    ->select();
                for($i=0;$i<count($list);$i++){
                    $res = Db::name('user')->where(array(
                        'id'=>$list[$i]['user_id']
                    ))->find();
                    $list[$i]['user_name']  = $res['name'];
                    $list[$i]['user_gangwei']  = $res['gangwei'];
                    $list[$i]['user_phone']  = $res['phone'];
                }
                $this->assign('list',$list);

            }
        }else{
            $count = DB::name('order')->count(); //计算记录数
            $limitRows = 10; // 设置每页记录数
            $p = new AjaxPage($count, $limitRows,"news"); //第三个参数是你需要调用换页的ajax函数名

            $limit_value = $p->firstRow . "," . $p->listRows;
            $list = DB::name('order')->order('uptime desc')->limit($limit_value)->select();
            $page = $p->show(); // 产生分页信息，AJAX的连接在此处生成

            for($i=0;$i<count($list);$i++){
                $res = Db::name('user')->where(array(
                    'id'=>$list[$i]['user_id']
                ))->find();
                $list[$i]['user_name']  = $res['name'];
                $list[$i]['user_gangwei']  = $res['gangwei'];
                $list[$i]['user_phone']  = $res['phone'];
            }
           
            $this->assign('list',$list);

            $this->assign('page',$page);
        }
        if(!empty($res['p'])){
            $p = $res['p'];
            $this->assign('p',$p);

        }


        return $this->fetch();
    }

  /*
   * 发送订阅消息
   */


    function getaccess_token(){

        $token = Db::name('token')
            ->where(array(
                'id'=>1
            ))
            ->find();
        if($token){
            if($token['expires']+$token['update_time']<time()){
                //appid与appsecret改成你自己的
                $appid = 'wxa7b85cb5e6a53427';
                $appsecret = '694c0aeb01b6b43bdc58cacbc1630cd9';
                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
                $data = curl_exec($ch);
                curl_close($ch);
                $data = json_decode($data,true);

//                $da['id'] = 1;
                $da['access_token'] =$data['access_token'];
                $da['update_time'] = time();
                Db::name('token')->where('id', 1)->update($da);

                return $data['access_token'];
            }else{
                return $token['access_token'];
            }

        }

    }


    //设置与发送模板信息
    function set_msg($openid,$title,$text,$time,$kehu){
        //获取access_token
        $access_token = $this->getaccess_token();
        //这里是在模板里修改相应的变量
        $openid =$openid;


        $post_data = array(
            "touser"=>$openid, //推送给谁,openid
            "template_id"=>"01qeqyaTMX4hqENQ7ibhiYw-PvM9BT2Ela4kbIs_9Fc", //微信后台的模板信息id
            "page"=>"index", //下面为预约
            "miniprogram_state"=>"developer",
            "lang"=>"zh_CN",
            "data"=> array(
                "thing1" => array(
                    "value"=>$title,
                ),
                "thing2"=>array(
                    "value"=>$text, //传的变量
                ),
                "date3"=>array(
                    "value"=>$time,
                ),
                "thing4"=> array(
                    "value"=>$kehu,
                )
            )
        );
        $post_data = json_encode($post_data);
//        print_r($post_data);

        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$access_token}";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
        $data = curl_exec($ch);
        curl_close($ch);
//        var_dump($data);
//         return $data;
    }

    //签约
    function set_msg1($openid,$title,$text,$time,$kehu){
        //获取access_token
        $access_token = $this->getaccess_token();
        //这里是在模板里修改相应的变量
        $openid =$openid;


        $post_data = array(
            "touser"=>$openid, //推送给谁,openid
            "template_id"=>"6vpTlQtye_O4OFqPMNlfJ1qyTX4ebOgdnI3pCUTMXLI", //微信后台的模板信息id
            "page"=>"index", //下面为预约
            "miniprogram_state"=>"developer",
            "lang"=>"zh_CN",
            "data"=> array(
                "thing2" => array(
                    "value"=>$title,
                ),
                "thing8"=>array(
                    "value"=>$kehu, //传的变量
                ),
                "phrase10"=>array(
                    "value"=>$text,
                ),
                "date9"=> array(
                    "value"=>$time,
                )
            )
        );
        $post_data = json_encode($post_data);
//        print_r($post_data);

        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$access_token}";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
        $data = curl_exec($ch);
        curl_close($ch);
//        var_dump($data);
//         return $data;
    }

    //认购
    function set_msg2($openid,$title,$text,$time){
        //获取access_token
        $access_token = $this->getaccess_token();
        //这里是在模板里修改相应的变量
        $openid =$openid;


        $post_data = array(
            "touser"=>$openid, //推送给谁,openid
            "template_id"=>"FV4H-xX90oytun4OilqZ-ABD6O_NBIspGq-c7FB3W3M", //微信后台的模板信息id
            "page"=>"index", //下面为预约
            "miniprogram_state"=>"developer",
            "lang"=>"zh_CN",
            "data"=> array(
                "thing2" => array(
                    "value"=>$title,
                ),
                "thing4"=>array(
                    "value"=>$text, //传的变量
                ),
                "time3"=> array(
                    "value"=>$time,
                )
            )
        );
        $post_data = json_encode($post_data);
//        print_r($post_data);

        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$access_token}";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
        $data = curl_exec($ch);
        curl_close($ch);
//        var_dump($data);
//         return $data;
    }
    //失效
    function set_msg3($openid,$title,$text,$time){
        //获取access_token
        $access_token = $this->getaccess_token();
        //这里是在模板里修改相应的变量
        $openid =$openid;


        $post_data = array(
            "touser"=>$openid, //推送给谁,openid
            "template_id"=>"jBmKVJ8gs3MzBAHQlNhMajjJOJDyFGQhpeohT2KRoLc", //微信后台的模板信息id
            "page"=>"index", //下面为预约
            "miniprogram_state"=>"developer",
            "lang"=>"zh_CN",
            "data"=> array(
                "thing2" => array(
                    "value"=>$title,
                ),
                "time5"=>array(
                    "value"=>$time, //传的变量
                ),
                "phrase7"=> array(
                    "value"=>$text,
                )
            )
        );
        $post_data = json_encode($post_data);
//        print_r($post_data);

        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$access_token}";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
        $data = curl_exec($ch);
        curl_close($ch);
        var_dump($data);
//         return $data;
    }


    /*
    * 到访
    */
    public function dao(){
        $map['id'] = $_POST['id'];
        $order = Db::name('order')->where(array(
            'id'=>$_POST['id']
        ))->find();
        $user =  Db::name('user')->where(array(
            'id'=>$order['user_id']
        ))->find();
        $_POST['dao'] = 1;
        $_POST['dao_time'] = date('Y-m-d H:i:s',time());
        $res = Db::name('order')->where($map)->update($_POST);
        if($res){
           $this->set_msg($user['openid'],$order['title'],'到访',date('Y-m-d H:i:s',time()),$order['username']);
            return show(1,'标注成功！');
        }else{
            return show(0,'标注失败！');
        }
    }
    /*
* 签约
*/
    public function qian(){
        $map['id'] = $_POST['id'];
        $order = Db::name('order')->where(array(
            'id'=>$_POST['id']
        ))->find();
        $user =  Db::name('user')->where(array(
            'id'=>$order['user_id']
        ))->find();
        $_POST['qian'] = 1;
        $_POST['qian_time'] = date('Y-m-d H:i:s',time());
        $res = Db::name('order')->where($map)->update($_POST);
        if($res){
            $this->set_msg1($user['openid'],$order['title'],'签约',date('Y-m-d H:i:s',time()),$order['username']);
            return show(1,'标注成功！');
        }else{
            return show(0,'标注失败！');
        }
    }

    /*
* 认购
*/
    public function ren(){
        $map['id'] = $_POST['id'];
        $order = Db::name('order')->where(array(
            'id'=>$_POST['id']
        ))->find();
        $user =  Db::name('user')->where(array(
            'id'=>$order['user_id']
        ))->find();
        $_POST['ren'] = 1;
        $_POST['ren_time'] = date('Y-m-d H:i:s',time());
        $res = Db::name('order')->where($map)->update($_POST);
        if($res){
            $this->set_msg2($user['openid'],$order['title'],'认购',date('Y-m-d H:i:s',time()));
            return show(1,'标注成功！');
        }else{
            return show(0,'标注失败！');
        }
    }

    /*
* 失效
*/
    public function shi(){
        $map['id'] = $_POST['id'];
        $order = Db::name('order')->where(array(
            'id'=>$_POST['id']
        ))->find();
        $user =  Db::name('user')->where(array(
            'id'=>$order['user_id']
        ))->find();
        $_POST['shi'] = 1;
        $_POST['shi_time'] = date('Y-m-d H:i:s',time());
        $res = Db::name('order')->where($map)->update($_POST);
        if($res){
            $this->set_msg3($user['openid'],$order['title'],'失效',date('Y-m-d H:i:s',time()));
            return show(1,'标注成功！');
        }else{
            return show(0,'标注失败！');
        }
    }


    /*
  * 取消到访
  */
    public function dao1(){
        $map['id'] = $_POST['id'];
        $_POST['dao'] = 0;
        $_POST['dao_time'] = date('Y-m-d H:i:s',time());
        $res = Db::name('order')->where($map)->update($_POST);
        if($res){
            return show(1,'取消标注成功！');
        }else{
            return show(0,'取消标注失败！');
        }
    }
    /*
* 取消签约
*/
    public function qian1(){
        $map['id'] = $_POST['id'];
        $_POST['qian'] = 0;
        $_POST['qian_time'] = date('Y-m-d H:i:s',time());
        $res = Db::name('order')->where($map)->update($_POST);
        if($res){
            return show(1,'取消标注成功！');
        }else{
            return show(0,'取消标注失败！');
        }
    }

    /*
* 取消认购
*/
    public function ren1(){
        $map['id'] = $_POST['id'];
        $_POST['ren'] = 0;
        $_POST['ren_time'] = date('Y-m-d H:i:s',time());
        $res = Db::name('order')->where($map)->update($_POST);
        if($res){
            return show(1,'取消标注成功！');
        }else{
            return show(0,'取消标注失败！');
        }
    }

    /*
* 取消失效
*/
    public function shi1(){
        $map['id'] = $_POST['id'];
        $_POST['shi'] = 0;
        $_POST['shi_time'] = date('Y-m-d H:i:s',time());
        $res = Db::name('order')->where($map)->update($_POST);
        if($res){
            return show(1,'取消标注成功！');
        }else{
            return show(0,'取消标注失败！');
        }
    }





}
